package top.pengdong.pictureShare.tweet.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import top.pengdong.pictureShare.common.model.PageUtils;
import top.pengdong.pictureShare.common.model.SysConstant;
import top.pengdong.pictureShare.tweet.aspect.TweetAspect;
import top.pengdong.pictureShare.tweet.dao.PsCountDao;
import top.pengdong.pictureShare.tweet.entity.PsCount;
import top.pengdong.pictureShare.tweet.entity.PsTweetEntity;
import top.pengdong.pictureShare.tweet.service.PsTweetService;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @description: 集成了获取 文章列表操作，和 统计文章的操作
 * @projectName: pictureShare
 * @see: top.pengdong.pictureShare.tweet.service.impl
 * @author: pc
 * @createTime: 2022/4/30 12:18
 * @version: 1.0
 */
@Slf4j
@Service
public class TimeSchedulingService {

	@Autowired
	private PsTweetService psTweetService;

	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private PsCountDao psCountDao;


	@PostConstruct
	public void init() throws Exception {
		log.info("排行榜单数据初始化开始...");
		GetHotSweets(1, 10);
		log.info("排行榜单数据已写入redis...");
	}


	@Scheduled(cron = "0 */5 * * * ? ")
	public void getHotTweetFirst() {
		GetHotSweets(1, 10);
	}

	public void GetHotSweets(Integer page, Integer limit) {
		log.info("开始更新 热点数据，最多关注数据，最多点赞数据");

		// 最多点赞
		Page<PsTweetEntity> PageTweets = psTweetService.page(
				new Page<>(page, limit),
				new QueryWrapper<PsTweetEntity>()
						.eq("is_publish", 1)
						.orderByDesc("like_count")
		);
		List<PsTweetEntity> count = PageTweets.getRecords();
		psTweetService.getTweetImages(count);
		redisTemplate.opsForValue().set(SysConstant.LIKE_SWEET + ":" + page + "_" + limit,
				new PageUtils(PageTweets), 70, TimeUnit.SECONDS);


		// 最多评论
		PageTweets = psTweetService.page(
				new Page<>(page, limit),
				new QueryWrapper<PsTweetEntity>()
						.eq("is_publish", 1)
						.orderByDesc("comment_count")
		);
		count = PageTweets.getRecords();
		psTweetService.getTweetImages(count);
		redisTemplate.opsForValue().set(SysConstant.COMMENT_SWEET + ":" + page + "_" + limit,
				new PageUtils(PageTweets), 70, TimeUnit.SECONDS);

		// 最多转发
		PageTweets = psTweetService.page(
				new Page<>(page, limit),
				new QueryWrapper<PsTweetEntity>()
						.eq("is_publish", 1)
						.orderByDesc("forward_count")
		);
		count = PageTweets.getRecords();
		psTweetService.getTweetImages(count);
		redisTemplate.opsForValue().set(SysConstant.FORWARD_SWEET + ":" + page + "_" + limit,
				new PageUtils(PageTweets), 70, TimeUnit.SECONDS);

		// 最多收藏
		PageTweets = psTweetService.page(
				new Page<>(page, limit),
				new QueryWrapper<PsTweetEntity>()
						.eq("is_publish", 1)
						.orderByDesc("collection_count")
		);
		count = PageTweets.getRecords();
		psTweetService.getTweetImages(count);
		redisTemplate.opsForValue().set(SysConstant.COLLECTION_SWEET + ":" + page + "_" + limit,
				new PageUtils(PageTweets), 70, TimeUnit.SECONDS);

		// 最多阅读
		PageTweets = psTweetService.page(
				new Page<>(page, limit),
				new QueryWrapper<PsTweetEntity>()
						.eq("is_publish", 1)
						.orderByDesc("view_count")
		);
		count = PageTweets.getRecords();
		psTweetService.getTweetImages(count);
		redisTemplate.opsForValue().set(SysConstant.HOT_SWEET + ":" + page + "_" + limit,
				new PageUtils(PageTweets), 70, TimeUnit.SECONDS);

		// TODO 热搜榜单 话题榜单设计 和 阅读量最多的文章

		log.info("更新 热点数据，最多关注数据，最多点赞数据结束");
	}


	/**
	 * 更新记录用户网站访问量
	 */
	@Scheduled(cron = "0 0 23 * * ? ")
	public void updateRedisToDatabase() {
		log.info(" —— —— 更新用户访问数据到数据库中");
		// TODO 记得建表进行用户访问量记录
		if (psCountDao.exists(new QueryWrapper<PsCount>().eq("id", TweetAspect.psCount.getId()))) {
			psCountDao.updateById(TweetAspect.psCount);
		} else {
			psCountDao.insert(TweetAspect.psCount);
		}
		log.info("完成 —— —— 更新用户访问数据到数据库");
	}

	/**
	 * 更新记录用户网站访问量
	 */
	//@Scheduled(cron = "*/10 * * * * ? ")
	public void updateRedis() {
		log.info(" —— —— 更新用户访问到redis中");
		// TODO 记得建表进行用户访问量记录
		redisTemplate.opsForHash().put(SysConstant.COUNT_SUM, String.valueOf(TweetAspect.psCount.getId()), TweetAspect.psCount);
		TweetAspect.psCount = (PsCount) redisTemplate.opsForHash().get(SysConstant.COUNT_SUM, String.valueOf(TweetAspect.psCount.getId()));
		log.info("完成 —— —— 更新用户访问到redis中");
	}

}
